package com.gomore.opple.module.addaddress;

import com.gomore.opple.R;
import com.gomore.opple.data.DataRepository;
import com.gomore.opple.data.remote.retrofit.HttpResponseFunc;
import com.gomore.opple.data.remote.retrofit.RxSubscriber;
import com.gomore.opple.data.remote.retrofit.ServerResponseFunc;
import com.gomore.opple.exception.ApiException;
import com.gomore.opple.rest.shippingaddr.RsShippingAddrEntity;
import com.gomore.opple.web.cgform.consumer.entity.TOConsumerEntity;
import com.gomore.opple.web.cgform.shippingaddr.entity.TOShippingAddrEntity;

import javax.inject.Inject;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class AddAddressPresenter implements AddAddressContract.Presenter {

    private DataRepository mDataRepositroy;
    private final AddAddressContract.View mView;
    private CompositeSubscription mSubscriptions;

    @Inject
    AddAddressPresenter(DataRepository dataRepository, AddAddressContract.View view) {
        mDataRepositroy = dataRepository;
        mView = view;
        mSubscriptions = new CompositeSubscription();
        mView.setPresenter(this);
    }


    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mSubscriptions.clear();
    }

    /**
     * 初始化数据
     */
    @Override
    public void prepareInitData() {
    }

    @Override
    public void saveAddress(TOShippingAddrEntity toShippingAddrEntity) {
        unsubscribe();
        mView.showProgressDialog();
        Subscription subscription = mDataRepositroy.saveShippingAddress(toShippingAddrEntity)
                .map(new ServerResponseFunc<String>())
                .onErrorResumeNext(new HttpResponseFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<String>() {
                    @Override
                    public void onNext(String string) {
                        super.onNext(string);
                        mView.hideProgressDialog();
                        mView.showMessage(R.string.save_success);
                        mView.backToAccount();
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public void updateAddress(TOShippingAddrEntity toShippingAddrEntity) {
        unsubscribe();
        mView.showProgressDialog();
        Subscription subscription = mDataRepositroy.updateShippingAddress(toShippingAddrEntity)
                .map(new ServerResponseFunc<String>())
                .onErrorResumeNext(new HttpResponseFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<String>() {
                    @Override
                    public void onNext(String string) {
                        super.onNext(string);
                        mView.hideProgressDialog();
                        mView.showMessage(R.string.save_success);
                        mView.backToAccount();
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public void removeAddress(String uuid) {
        unsubscribe();
        mView.showProgressDialog();
        Subscription subscription = mDataRepositroy.removeShippingAddress(uuid)
                .map(new ServerResponseFunc<String>())
                .onErrorResumeNext(new HttpResponseFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<String>() {
                    @Override
                    public void onNext(String string) {
                        super.onNext(string);
                        mView.hideProgressDialog();
                        mView.showMessage(R.string.delete_success);
                        mView.backToAccount();
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public TOConsumerEntity getConsumer() {
        return mDataRepositroy.getConsumer();
    }

}
